package com.yanze.schoolinformation.view.community.circle;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.databinding.DataBindingUtil;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

import com.example.xrecyclerview.XRecyclerView;
import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.adapter.rv.BookAdapter;
import com.yanze.schoolinformation.app.AppConfig;
import com.yanze.schoolinformation.base.BaseActivity;
import com.yanze.schoolinformation.databinding.ActivitySecondBookBinding;
import com.yanze.schoolinformation.model.bean.resp.RespBook;
import com.yanze.schoolinformation.model.bean.resp.RespUser;
import com.yanze.schoolinformation.utils.CommonUtils;
import com.yanze.schoolinformation.utils.PerfectClickListener;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.viewModule.circle.SecondBookViewModel;
import com.yanze.schoolinformation.viewModule.circle.SecondBookViewNavigator;

public class SecondBookActivity extends BaseActivity<ActivitySecondBookBinding> implements SecondBookViewNavigator {

    private BookAdapter mAdapter;
    private SecondBookViewModel viewModel;
    private LocalBroadcastManager localBroadcastManager;
    private BookReceive mReceive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_book);
        setTitle("二手书");
        viewModel = new SecondBookViewModel(this);
        viewModel.setmNavigator(this);
        initView();
        viewModel.getAllBook();

        //注册本地广播
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        mReceive = new BookReceive();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.schoolinformation.publishdynamic");
        localBroadcastManager.registerReceiver(mReceive, intentFilter);
    }

    private void initView() {
        bindingView.srlRefresh.setColorSchemeColors(CommonUtils.getColor(R.color.colorTheme));
        bindingView.srlRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                bindingView.srlRefresh.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        viewModel.setmPage(1);
                        viewModel.getAllBook();
                    }
                }, 1000);
            }
        });

        bindingView.xrvBook.setPullRefreshEnabled(false);
        bindingView.xrvBook.setLoadingMoreEnabled(true);
        bindingView.xrvBook.setLayoutManager(new LinearLayoutManager(this));
        bindingView.xrvBook.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
            }

            @Override
            public void onLoadMore() {
                viewModel.setmPage(viewModel.getmPage() + 1);
                viewModel.getAllBook();
            }
        });
        mAdapter = new BookAdapter(this);
        bindingView.xrvBook.setAdapter(mAdapter);

        bindingView.fabAdd.setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                RespUser.UserInfo user = AppConfig.getUserInfo(SecondBookActivity.this);
                if (user != null) {
                    PublishDynamicActivity.start(SecondBookActivity.this, 2);
                } else {
                    ToastUtils.show(SecondBookActivity.this, "登陆之后才能发布二手书信息~");
                }
            }
        });
    }

    @Override
    public void showLoadSuccessView() {
        showContentView();
    }

    @Override
    public void onRefreshSuccess(RespBook entity) {
        if (bindingView.srlRefresh.isRefreshing()) {
            bindingView.srlRefresh.setRefreshing(false);
        }

        mAdapter.clear();
        mAdapter.addAll(entity.getData());
        mAdapter.notifyDataSetChanged();
        bindingView.xrvBook.refreshComplete();
    }

    @Override
    public void onLoadMoreSuccess(RespBook entity) {
        mAdapter.addAll(entity.getData());
        mAdapter.notifyDataSetChanged();
        bindingView.xrvBook.refreshComplete();
    }

    @Override
    public void showNoMoreLoading() {
        bindingView.xrvBook.noMoreLoading();
    }

    @Override
    public void showLoadFailedView() {
        if (bindingView.srlRefresh.isRefreshing()) {
            bindingView.srlRefresh.setRefreshing(false);
        }
        if (mAdapter.getItemCount() == 0) {
            showError();
        }
    }

    @Override
    protected void onRefresh() {
        super.onRefresh();
        viewModel.getAllBook();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(mReceive);
        viewModel.onDestory();
    }

    private class BookReceive extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            viewModel.setmPage(1);
            viewModel.getAllBook();
        }
    }

    /**
     * 入口
     */
    public static void start(Context context) {
        Intent intent = new Intent(context, SecondBookActivity.class);
        context.startActivity(intent);
    }
}
